﻿using System;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;

namespace JYH.Framework
{
    class DrawCalendar
    {
        private Dictionary<DateTime, string> _AppendText;
        /// <summary>
        /// 向指定日期中添加内容集合
        /// </summary>
        public Dictionary<DateTime, string> AppendText
        {
            get { return _AppendText; }
            set { _AppendText = value; }
        }


        private string ConvertToChs(int Num)
        {
            switch (Num)
            {
                case 0:
                    return "<font color='red'><strong>日</strong></font>";
                case 1:
                    return "<strong>一</strong>";
                case 2:
                    return "<strong>二</strong>";
                case 3:
                    return "<strong>三</strong>";
                case 4:
                    return "<strong>四</strong>";
                case 5:
                    return "<strong>五</strong>";
                case 6:
                    return "<font color='red'><strong>六</strong></font>";
                default:
                    return "";
            }
        }


        private HtmlTable GetHtmlTable(string tableWidth)
        {
            HtmlTable hTable = new HtmlTable();
            hTable.Attributes.Add("border", "0");
            hTable.Attributes.Add("cellpadding", "1");
            hTable.Attributes.Add("cellspacing", "1");
            hTable.Style.Add("background", "#c8c8c8");
            hTable.Style.Add("width", tableWidth);
            return hTable;
        }

        /// <summary>
        /// 日历表头
        /// </summary>
        /// <param name="hTable"></param>
        private void SetTableHead(HtmlTable hTable)
        {
            HtmlTableRow hRow = new HtmlTableRow();
            hRow.Style.Add("background", "#F0F0F0");
            HtmlTableCell hCell;
            for (int i = 0; i < 7; i++)
            {
                hCell = new HtmlTableCell();
                hCell.Attributes.Add("align", "center");
                hCell.Attributes.Add("height", "25px");
                hCell.InnerHtml = ConvertToChs(i);
                hRow.Controls.Add(hCell);
            }
            hTable.Controls.Add(hRow);
        }



        private HtmlTableCell GetHTCell(DateTime dt)
        {
            HtmlTableCell hCell = new HtmlTableCell();
            hCell.Attributes.Add("width", "14.3%");
            hCell.Attributes.Add("height", "100px");
            hCell.Attributes.Add("align", "left");
            hCell.Attributes.Add("valign", "top");

            hCell.Attributes.Add("onmouseover", "this.style.background='#f1ffff'");
            hCell.Attributes.Add("onmouseout", "this.style.background='#ffffff'");
            if (dt.DayOfWeek == DayOfWeek.Saturday || dt.DayOfWeek == DayOfWeek.Sunday)
            {
                if (dt == DateTime.Today)
                    hCell.InnerHtml = "<font color='#008000' size='6px'><strong><i>" + dt.ToString("dd") + "</i></strong></font>";
                else
                    hCell.InnerHtml = "<font color='red'><strong>" + dt.ToString("dd") + "</strong></font>";
            }
            else
            {
                if (dt == DateTime.Today)  //
                    hCell.InnerHtml = "<font color='#008000' size='6px'><strong><i>" + dt.ToString("dd") + "</i></strong></font>";
                else
                    hCell.InnerHtml = dt.ToString("dd");
            }
            //按日期附加单元格中的内容
            if (AppendText != null)
            {
                foreach (KeyValuePair<DateTime, string> entry in AppendText)
                {
                    if (entry.Key == dt)
                        hCell.InnerHtml += "<br>" + entry.Value;
                }
            }
            return hCell;
        }


        /// <summary>
        /// HTML日历
        /// </summary>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="Width">日历宽度</param>
        /// <returns>HtmlTable</returns>
        public string Calendar(int year, int month, string Width)
        {
            HtmlTable hTable = GetHtmlTable(Width); //定义Table
            HtmlTableRow hRow;
            HtmlTableCell hCell;
            SetTableHead(hTable);//添加表头

            DateTime dtMonth1 = Convert.ToDateTime(year.ToString() + "-" + month.ToString() + "-01");//每月一号
            int weekOnMonth1 = Convert.ToInt32(dtMonth1.DayOfWeek);//每月一号是周几
            hRow = new HtmlTableRow();
            hRow.Style.Add("background", "#FFFFFF");
            for (int i = 0; i < weekOnMonth1; i++)
            {
                hCell = new HtmlTableCell();
                hCell.InnerHtml = "-";
                hRow.Controls.Add(hCell);
            }
            int dayOfMonth = int.Parse(dtMonth1.AddMonths(1).AddDays(-1).ToString("dd")) + weekOnMonth1;
            int temp = weekOnMonth1;
            while (temp < dayOfMonth)
            {
                if (temp % 7 == 0)
                {
                    hTable.Controls.Add(hRow);
                    hRow = new HtmlTableRow();
                    hRow.Style.Add("background", "#FFFFFF");
                    hCell = GetHTCell(dtMonth1);
                    dtMonth1 = dtMonth1.AddDays(1);
                    hRow.Controls.Add(hCell);
                }
                else
                {
                    hCell = GetHTCell(dtMonth1);
                    dtMonth1 = dtMonth1.AddDays(1);
                    hRow.Controls.Add(hCell);
                }
                temp++;
            }

            dtMonth1 = Convert.ToDateTime(year.ToString() + "-" + month.ToString() + "-01");//每月一号		
            int tp = Convert.ToInt32(dtMonth1.AddMonths(1).AddDays(-1).DayOfWeek);//每月月终是周几，填充空白
            for (int i = tp + 1; i < 7; i++)
            {
                hCell = new HtmlTableCell();
                hCell.InnerHtml = "-";
                hRow.Controls.Add(hCell);
            }
            hTable.Controls.Add(hRow);
            return hTable.InnerHtml;
        }

    }
}
